//package com.gwgreenway.oauth2.controller;
//
//import User;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.client.ServiceInstance;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.wechat.provider.OAuth2Authentication;
//import org.springframework.security.wechat.provider.authentication.BearerTokenExtractor;
//import org.springframework.security.wechat.provider.token.ResourceServerTokenServices;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.servlet.ServletRequest;
//import javax.servlet.http.HttpServletRequest;
//import java.io.IOException;
//import java.security.Principal;
//
///**
// * Created by Administrator on 2019/4/1.
// */
//@RestController
//public class ResourceController {
//
//
//    private final static Logger logger = LoggerFactory.getLogger(ResourceController.class);
//    BearerTokenExtractor tokenExtractor = new BearerTokenExtractor();
//
//    private final static String DEFAULT_NAME = "xuan";
//    private static String DEFAULT_SERVICE_ID = "application";
//    private static String DEFAULT_HOST = "localhost";
//    private static int DEFAULT_PORT = 8080;
//
//
//    @Autowired
//    ResourceServerTokenServices tokenServices;
//
//
//    // 拦截资源
//    @RequestMapping(value = "/wechat/{userId}", method = RequestMethod.GET)
//    public User getUserByUserId(@PathVariable("userId") int userId) {
//        logger.info("Get User by UserId {}", userId);
//        return new User(userId, DEFAULT_NAME, "11");
//    }
//
//    // 不拦截资源
//    @RequestMapping(value = "/instance/{serviceId}", method = RequestMethod.GET)
//    public String getInstanceByServiceId(@PathVariable("serviceId") String serviceId) {
//        logger.info("Get Instance by serviceId {}", serviceId);
//        return serviceId;
//    }
//
//
//    @RequestMapping(value = "/wechat")
//    public Principal userInfo(ServletRequest req) throws IOException {
//
//        final HttpServletRequest request = (HttpServletRequest) req;
//        Authentication authentication = tokenExtractor.extract(request);
//        String token = (String) authentication.getPrincipal();
//        OAuth2Authentication oAuth2Authentication = tokenServices.loadAuthentication(token);
//        return oAuth2Authentication;
//
//    }
//
//
//}